package com.sync.syncmasterdata.outsystem.controller;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.sync.syncmasterdata.dingrobot.service.DingAPIService;
import com.sync.syncmasterdata.dingrobot.service.DingRobotService;
import com.sync.syncmasterdata.outsystem.entity.CrmContactErpAnmEntity;
import com.sync.syncmasterdata.outsystem.entity.CusDeliverAddErpAnmEntity;
import com.sync.syncmasterdata.outsystem.entitymbt.CrmContactErpMBTEntity;
import com.sync.syncmasterdata.outsystem.entitymbt.CusDeliverAddErpMBTEntity;
import com.sync.syncmasterdata.outsystem.service.CustomerAddAndContactService;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static com.sync.syncmasterdata.util.NormalUtil.*;

/**
 * 体外公司客户信息同步
 */

@RestController
@RequestMapping("/outSystem/customerSync")
@Slf4j
public class OutCustomerSyncController {

    //钉钉服务
    @Autowired
    private DingRobotService dingRobotService;

    @Autowired
    private ObjectMapper objectMapper;

    //服务层
    @Autowired
    private CustomerAddAndContactService customerAddAndContactService;

    private List<String> msgUsers;
    private List<String> msgCWUsers;

    public OutCustomerSyncController(DingAPIService dingAPIService) {
        this.msgUsers = dingAPIService.convertPhoneToUseridList(Arrays.asList("15267167276"));
        //提醒 管理相关人员 添加客户信息
        this.msgCWUsers=dingAPIService.convertPhoneToUseridList(Arrays.asList("15267167276","15903019297"));
    }

    /**
     * 新增客户 联系人信息
     * add=新增 edit=修改
     */
    @PostMapping("/pushCustomerContact")
    @ResponseBody
    public void pushCustomerContact(@RequestBody Map<String,Object> reqJson) {
        try {
            log.info("获取到的json=====>>{}", reqJson.toString());
            CrmContactErpAnmEntity entity = new CrmContactErpAnmEntity();
            entity.setCContactCode(String.valueOf(System.currentTimeMillis()));
            entity.setCCusCode(reqJson.getOrDefault("cCusCode", "").toString());

            entity.setCContactName(reqJson.getOrDefault("cContactName","").toString());
            entity.setCMobilePhone(reqJson.getOrDefault("cMobilePhone", "").toString());
            String guid = String.format("{%s}", UUID.randomUUID().toString().toUpperCase());
            entity.setOID(guid);
//            entity.setUfts(generateHexTimestamp());
            entity.setDCreateDate(getNowTime());
            customerAddAndContactService.createCrmContractToU8(entity);
            //美博特推送
            CrmContactErpMBTEntity entityMBT = new CrmContactErpMBTEntity();
            entityMBT.setCContactCode(String.valueOf(System.currentTimeMillis()));
            entityMBT.setCCusCode(reqJson.getOrDefault("cCusCode", "").toString());
            entityMBT.setCContactName(reqJson.getOrDefault("cContactName","").toString());
            entityMBT.setCMobilePhone(reqJson.getOrDefault("cMobilePhone", "").toString());
            entityMBT.setOID(guid);
//            entity.setUfts(generateHexTimestamp());
            entityMBT.setDCreateDate(getNowTime());
            customerAddAndContactService.createCrmContractToU8(entityMBT);
            log.info("新增完成====>>>>");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("新增异常====>>>>");
        }
    }

    /**
     * 新增客户 收货地址
     * add=新增 edit=修改
     */
    @PostMapping("/pushCustomerAddress")
    @ResponseBody
    public void pushCustomerAddress(@RequestBody Map<String,Object> reqJson) {
        try {
            log.info("获取到的json=====>>{}", reqJson.toString());
            CusDeliverAddErpAnmEntity entity = new CusDeliverAddErpAnmEntity();
            entity.setCCusAddressGUID(UUID.randomUUID().toString().toUpperCase());
            entity.setCAddCode(String.valueOf(System.currentTimeMillis()));
            entity.setBDefault(0);
            entity.setCCusCode(reqJson.getOrDefault("cCusCode","").toString());
            entity.setCDeliverAdd(reqJson.getOrDefault("cDeliverAdd","").toString());
//            entity.setUfts(generateHexTimestamp());
            customerAddAndContactService.createShippingAddressToU8(entity);
            //美博特推送
            CusDeliverAddErpMBTEntity entityMBT = new CusDeliverAddErpMBTEntity();
            entityMBT.setCCusAddressGUID(UUID.randomUUID().toString().toUpperCase());
            entityMBT.setCAddCode(String.valueOf(System.currentTimeMillis()));
            entityMBT.setBDefault(0);
            entityMBT.setCCusCode(reqJson.getOrDefault("cCusCode","").toString());
            entityMBT.setCDeliverAdd(reqJson.getOrDefault("cDeliverAdd","").toString());
            customerAddAndContactService.createShippingAddressToU8(entityMBT);
            log.info("新增完成====>>>>");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("新增异常====>>>>");
        }
    }

    /**
     * 生成类似于 0x0000000000A542E5 的时间戳
     * @return
     */
    private String generateHexTimestamp() {
        long currentTimeMillis = System.currentTimeMillis();
        return String.format("0x%016X", currentTimeMillis);
    }




}
